package com.example.spring;

import com.example.spring.controller.ProductController;
import com.example.spring.controller.UserController;
import com.example.spring.dao.ProductDao;
import com.example.spring.dao.UserDao;
import com.example.spring.service.ProductService;
import com.example.spring.service.UserService;
import com.example.spring.service.impl.UserDaoImpl;
import com.example.spring.service.impl.UserServiceImpl;

public class TransactionFrameworkDemo {
    public static void main(String[] args) {
        // 创建DAO实例
        UserDao userDao = new UserDaoImpl();
        ProductDao productDao = new ProductDao("MySQL");

        // 创建Service实例并代理
        UserService userService = TransactionManager.getProxy(new UserServiceImpl(userDao));
        ProductService productService = TransactionManager.getProxy(new ProductService(productDao));

        // 创建Controller
        UserController userController = new UserController(userService);
        ProductController productController = new ProductController(productService);

        System.out.println("===== 测试有接口的Service (JDK动态代理) =====");
        userController.register("张三");
        System.out.println("--------------------");
        userController.update("李四");

        System.out.println("\n===== 测试无接口的Service (CGLIB代理) =====");
        productController.create("手机");
        System.out.println("--------------------");
        productController.modify("电脑");
        System.out.println("--------------------");
        productController.modify("error");
    }
}